﻿
using System;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSQLObjects.Model;
using NSQLObjects.Persistence;
using NSQLObjectsTests.DBModel;

namespace NSQLObjectsTests
{
    [TestClass]
    public class AttributeTests
    {
        [TestMethod]
        public void CustomerAttributeTests() {
            Type tCustomer = typeof(ProtectedCustomer);
            var piID = tCustomer.GetProperty("ID", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            Assert.IsNotNull(piID);
            Assert.AreEqual(MethodAttributes.Private, piID.GetSetMethod(true).Attributes & MethodAttributes.Private);
            var fi = piID.GetCustomAttributes(typeof(FieldAttribute), false).Cast<FieldAttribute>().FirstOrDefault();
            Assert.IsNotNull(fi);
            Assert.AreEqual(FieldRoles.Compare | FieldRoles.Immutable, fi.Role);
            Assert.IsTrue(fi.IncludedFor(ParameterSets.InsertValues));
            Assert.IsTrue(fi.IncludedFor(ParameterSets.UpdateCompares));
            Assert.IsTrue(fi.IncludedFor(ParameterSets.DeleteCompares));
            Assert.IsFalse(fi.IncludedFor(ParameterSets.UpdateValues));
            var piName = tCustomer.GetProperty("Name", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            Assert.IsNotNull(piName);
            Assert.AreEqual(MethodAttributes.Family, piName.GetSetMethod(true).Attributes & MethodAttributes.Family);
            fi = piName.GetCustomAttributes(typeof(FieldAttribute), false).Cast<FieldAttribute>().FirstOrDefault();
            Assert.IsNotNull(fi);
            Assert.AreEqual(FieldRoles.None, fi.Role);
            Assert.IsTrue(fi.IncludedFor(ParameterSets.InsertValues));
            Assert.IsFalse(fi.IncludedFor(ParameterSets.UpdateCompares));
            Assert.IsFalse(fi.IncludedFor(ParameterSets.DeleteCompares));
            Assert.IsTrue(fi.IncludedFor(ParameterSets.UpdateValues));

            var piAddress = tCustomer.GetProperty("Address", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            Assert.IsNotNull(piAddress);
            Assert.IsNotNull(piName.GetSetMethod());
            fi = piAddress.GetCustomAttributes(typeof(FieldAttribute), false).Cast<FieldAttribute>().FirstOrDefault();
            Assert.IsNotNull(fi);
            Assert.AreEqual(FieldRoles.Readonly | FieldRoles.Compare, fi.Role);
            Assert.IsFalse(fi.IncludedFor(ParameterSets.InsertValues));
            Assert.IsTrue(fi.IncludedFor(ParameterSets.UpdateCompares));
            Assert.IsTrue(fi.IncludedFor(ParameterSets.DeleteCompares));
            Assert.IsFalse(fi.IncludedFor(ParameterSets.UpdateValues));

        }
    }
}
